package com.atserver.windows;

import com.atserver.listener.CheckBoxChangeListener;
import com.atserver.listener.FocusMenuActionListener;
import com.atserver.listener.TextFieldFocusListener;
import com.atserver.listener.TextFieldKeyListener;
import lombok.Getter;

import javax.swing.*;
import java.awt.*;

/**
 * @program: atserver
 * @Description:
 * @author: Mr.Cheng
 * @date: 2022/5/10 11:08 上午
 */
@Getter
public class ScannerWindow extends JFrame {
    private JPanel jPanel;
    private JButton submitButton;
    private JTextField inputBarCodeTextField;
    private JTextField xCoordinateTextField;
    private JTextField yCoordinateTextField;

    /**
     * Creates a new, initially invisible <code>Frame</code> with the
     * specified title.
     * <p>
     * This constructor sets the component's locale property to the value
     * returned by <code>JComponent.getDefaultLocale</code>.
     *
     * @param title the title for the frame
     * @throws HeadlessException if GraphicsEnvironment.isHeadless()
     *                           returns true.
     * @see GraphicsEnvironment#isHeadless
     * @see Component#setSize
     * @see Component#setVisible
     * @see JComponent#getDefaultLocale
     */
    public ScannerWindow(String title) throws HeadlessException {
        super(title);
        init();
    }

    public void init(){
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(270,150);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

        jPanel = new JPanel();

        JLabel jLabel = new JLabel("条形码：");
        JLabel xjLabel = new JLabel("x坐标：");
        JLabel yjLabel = new JLabel("y坐标：");
        inputBarCodeTextField = new JTextField(15);
        xCoordinateTextField = new JTextField(5);
        xCoordinateTextField.setEnabled(false);
        yCoordinateTextField = new JTextField(5);
        yCoordinateTextField.setEnabled(false);
        submitButton = new JButton("提交");

        /*
         * 创建一个菜单栏
         */
        JMenuBar menuBar = new JMenuBar();

        /** 创建一级菜单 **/
        JMenu fileMenu = new JMenu("文件");
        JMenu focusMenu = new JMenu("焦点");
        JMenu uploadMenu = new JMenu("上传");
        JMenu aboutMenu = new JMenu("关于");

        /** 创建二级菜单 **/
        final JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("设置焦点");
        JMenuItem openNewFocus = new JMenuItem("获取新焦点");
        checkBoxMenuItem.setSelected(false);
        focusMenu.add(checkBoxMenuItem);
        focusMenu.addSeparator();
        focusMenu.add(openNewFocus);

        /** 添加监听 **/
        checkBoxMenuItem.addChangeListener(new CheckBoxChangeListener(this,checkBoxMenuItem));
        openNewFocus.addActionListener(new FocusMenuActionListener(this));


        /** 一级菜单添加到菜单栏 **/
        menuBar.add(fileMenu);
        menuBar.add(focusMenu);
        menuBar.add(uploadMenu);
        menuBar.add(aboutMenu);

        inputBarCodeTextField.addFocusListener(new TextFieldFocusListener(this));
        inputBarCodeTextField.addKeyListener(new TextFieldKeyListener(this));

        jPanel.add(xjLabel);
        jPanel.add(xCoordinateTextField);
        jPanel.add(yjLabel);
        jPanel.add(yCoordinateTextField);
        jPanel.add(jLabel);
        jPanel.add(inputBarCodeTextField);
        jPanel.add(submitButton);

        setJMenuBar(menuBar);
        this.setContentPane(jPanel);
    }
}
